package com.zut.hospital.service.staff;

import com.alibaba.fastjson.JSON;
import com.zut.hospital.dao.diagnose.PatientMapper;
import com.zut.hospital.dao.staff.DutyMapper;
import com.zut.hospital.dao.staff.OfficeMapper;
import com.zut.hospital.dao.staff.UserMapper;
import com.zut.hospital.entity.staff.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author hyx
 * @date 2019/5/22 9:45
 * @description TODO
 * @Version
 */

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    OfficeMapper officeMapper;
    @Autowired
    PatientMapper patientMapper;
    @Autowired
    DutyMapper dutyMapper;

    public String layuiJsonParse1(List <Map <String, String>> list, int size) {
        Map<String, Object> layuiMap = new HashMap<>();
        layuiMap.put("code", 0);
        layuiMap.put("msg", "success");
        layuiMap.put("count", size);
        layuiMap.put("data", list);
        return JSON.toJSONString(layuiMap);
    }
    public Map selectAllUser(User user){
        Map<String,String>map;

        UserExample userExample=new UserExample();
        userExample.createCriteria().andIdEqualTo(user.getId());
        user = userMapper.selectByExample(userExample).get(0);


        OfficeExample officeExample=new OfficeExample();
        officeExample.createCriteria().andIdEqualTo(user.getOfcId());
        Office office=officeMapper.selectByExample(officeExample).get(0);

        map=new HashMap<>();
        map.put("id",user.getId().toString());
        map.put("ofc_id",office.getId().toString());
        map.put("ad_num",user.getAdNum());
        map.put("ad_password",user.getAdPassword());
        map.put("ad_remark",user.getAdRemark());

        if(user.getAdPower()==1)
            map.put("ad_power","超级管理员");//权限需要判断
        else
            map.put("ad_power","普通管理员");

        map.put("ad_name",user.getAdName());
        return map;
    }


    @Override
    public String getAllUser() {
        UserExample userExample=new UserExample();
        List<User> users=userMapper.selectByExample(userExample);
        Map<String,String> userMap;
        List<Map<String,String>> userList=new ArrayList<>();
        for (User user:users) {
            userMap=new HashMap<>();
            userMap.put("userId",user.getId().toString());
            userMap.put("ad_name", user.getAdName());
            userList.add(userMap);
        }
        return JSON.toJSONString(userList);
    }

    @Override
    public String pageUser(int currPage,int pageSize){
        UserExample userExample=new UserExample();
        List<User>userList= userMapper.selectByExample(userExample);
        int size=userList.size();

        List<Map<String,String>>resaultmap=new ArrayList<>();
        int firstIndex=(currPage-1)*pageSize;
        int lastIndex=currPage*pageSize;
        if(currPage*pageSize>size){
            lastIndex=size;
        }
        for (User user:userList.subList(firstIndex,lastIndex)){
            resaultmap.add(selectAllUser(user));
        }
        return layuiJsonParse(resaultmap,size);
    }

    @Override
    public int addUser(Integer ofc_id, String ad_num, String ad_password, String ad_remark, Integer ad_power, String ad_name) {
        OfficeExample officeExample=new OfficeExample();
        officeExample.createCriteria().andIdEqualTo(ofc_id);
        List<Office> offices=officeMapper.selectByExample(officeExample);
        if(offices.size()==0)
            return 0;
        Office office=offices.get(0);
        ofc_id=office.getId();

        User user=new User();
        user.setOfcId(ofc_id);
        user.setAdNum(ad_num);
        user.setAdPassword(ad_password);
        user.setAdRemark(ad_remark);
        user.setAdPower(ad_power);
        user.setAdName(ad_name);

        userMapper.insert(user);

        return 1;
    }





    @Override
    public String findByOfcId(int currPage,int pageSize,Integer ofcId) {
        List<User>userList=userMapper.selectByOfcId(ofcId);
        List<Map<String,String>>resaultmap=new ArrayList<>();
        int a=0;
        for (User user:userList){
            System.out.println("ofcId:"+user.getOfcId());

            UserExample userExample=new UserExample();
            userExample.createCriteria().andIdEqualTo(user.getOfcId());
            List<User>userList1=userMapper.selectByExample(userExample);
            int size=userList1.size();
            a=size;
            System.out.println(a);
            int firstIndex=(currPage-1)*pageSize;//每一页第一项
            int lastIndex=currPage*pageSize;//每一页最后一项
            if(currPage*pageSize>size){//最后一页
                lastIndex=size;
            }
            for (User user1:userList1.subList(firstIndex,lastIndex)){
                resaultmap.add(selectAllUser(user1));
            }
        }
        System.out.println(resaultmap.toString());

        return layuiJsonParse(resaultmap,a);
    }

    @Override
    public int updateUser(int id, String field, String value) {

        UserExample userExample=new UserExample();
        userExample.createCriteria().andIdEqualTo(id);
        User user=userMapper.selectByExample(userExample).get(0);

        userMapper.updateByExampleSelective(user,userExample);
        //userMapper.updateByExample(user,userExample);
        return 1;
    }

    @Override
    public String duty() {


        Map<String,String>map;
        DutyExample dutyExample=new DutyExample();
        List<Duty>duties=dutyMapper.selectByExample(dutyExample);
        List<Map<String,String>>resaultmap=new ArrayList<>();

        for (Duty duty:duties){
            map=new HashMap <>();
            UserExample userExample=new UserExample();
            userExample.createCriteria().andIdEqualTo(duty.getUserId());
            User user=userMapper.selectByExample(userExample).get(0);
            map.put("name",user.getAdName());
            map.put("weizhi",duty.getDutyTime().toString());
            map.put("remark",duty.getRemark());
            resaultmap.add(map);
        }
return layuiJsonParse1(resaultmap,resaultmap.size());
    }


    @Override
    public int updateduty(String field,String value) {
        int weizhi;
        String remark;
        Integer user_id;

        DutyExample dutyExample=new DutyExample();
        Duty duty=dutyMapper.selectByExample(dutyExample).get(0);

        UserExample userExample=new UserExample();
        userExample.createCriteria().andIdEqualTo(duty.getUserId());

        if (field.equals("weizhi")&&field.equals("remark")){
            weizhi=Integer.parseInt(value);
            remark=String.valueOf(value);
            duty.setDutyTime(weizhi);
            duty.setRemark(remark);
        }
        else {
            user_id=Integer.parseInt(value);
            duty.setUserId(user_id);

        }
        return dutyMapper.updateByExampleSelective(duty,dutyExample);
    }


    @Override
    public int deleteUser(String ad_num) {
        UserExample userExample=new UserExample();
        userExample.createCriteria().andAdNumEqualTo(ad_num);
        userMapper.deleteByExample(userExample);
        return 1;
    }
    @Override
    public String layuiJsonParse(List <Map <String, String>> list, int size) {
        Map<String, Object> layuiMap = new HashMap<>();
        layuiMap.put("code", 0);
        layuiMap.put("msg", "success");
        layuiMap.put("count", size);
        layuiMap.put("data", list);
        return JSON.toJSONString(layuiMap);
    }



}

